package com.gxsyh.nantie.statements.management.provider.service.impl;

import com.gxsyh.nantie.statements.management.provider.domain.ApartmentStatements;
import com.gxsyh.nantie.statements.management.provider.domain.TbDepartment;
import com.gxsyh.nantie.statements.management.provider.domain.TbOrder;
import com.gxsyh.nantie.statements.management.provider.service.ApartmentStatementsService;
import com.gxsyh.nantie.statements.management.provider.service.TbDepartmentService;
import com.gxsyh.nantie.statements.management.provider.service.TbOrderService;
import com.gxsyh.nantie.statements.management.provider.util.ApartmentOrderCountUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ApartmentStatementsServiceImpl implements ApartmentStatementsService {

    @Resource
    private TbOrderService tbOrderService;

    @Resource
    private TbDepartmentService tbDepartmentService;



    @Override
    public List<ApartmentStatements> selectSubordinateApartmentStatements(Long departmentId,Long parentId) {
        List<ApartmentStatements> apartmentStatementsList=new ArrayList<>();
        List<TbDepartment> tbDepartmentList=tbDepartmentService.selectDepartmentAndSubordinate(1,departmentId,parentId);
        ApartmentOrderCountUtil apartmentOrderCountUtil=new ApartmentOrderCountUtil();
        for (TbDepartment list:tbDepartmentList)
        {
            List<TbOrder> tbOrderList=tbOrderService.selectOrder(list.getId());
            apartmentStatementsList.add(apartmentOrderCountUtil.count(list.getId(),list.getName(),tbOrderList));
        }
        return apartmentStatementsList;
    }

    @Override
    public List<ApartmentStatements> selectApartmentStatements(Long departmentId) {
        List<ApartmentStatements> apartmentStatementsList=new ArrayList<>();
        List<TbDepartment> tbDepartments=tbDepartmentService.selectDepartmentSubordinate(1,departmentId);
        ApartmentOrderCountUtil apartmentOrderCountUtil=new ApartmentOrderCountUtil();
        for (TbDepartment list:tbDepartments)
        {
            List<ApartmentStatements> apartmentStatementsList2=new ArrayList<>();
            List<TbDepartment> tbDepartments2=tbDepartmentService.selectDepartmentAndSubordinate(1,list.getId(),list.getId());
            for (TbDepartment list2:tbDepartments2)
            {
                List<TbOrder> tbOrderList=tbOrderService.selectOrder(list2.getId());
                apartmentStatementsList2.add(apartmentOrderCountUtil.count(list2.getId(),list2.getName(),tbOrderList));
            }
            ApartmentStatements apartmentStatements=apartmentOrderCountUtil.SummaryCount(list.getId(),list.getName(),apartmentStatementsList2);
            apartmentStatementsList.add(apartmentStatements);
        }
        return apartmentStatementsList;
    }
}